package server.phone.dao;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.utopia84.model.Bill;
import com.utopia84.model.Billandpdt;

public class BillAndPdtDAO {
	DBHelper manager;
	String sql = "";
	ResultSet rs;

	public BillAndPdtDAO() throws IOException, ClassNotFoundException, SQLException {
		manager = DBHelper.createInstance();
	}

	public int Add(Billandpdt bap) throws SQLException {
		int result = 0;
		sql = "insert into Bill(id,billId,salerecordId,pdtCode) values(?,?,?,?)";
		Object[] params = new Object[4];

		params[0] = "";
		params[1] = bap.getBillId();
		params[2] = bap.getSalerecordId();
		params[3] = bap.getPdtCode();

		manager.connectDB();
		result = manager.insertBillAndPdt(sql, params);
		manager.closeDB();
		return result;
	}

	public List<Billandpdt> GetBillAndPdt(String billId,String salerecordId){
		List<Billandpdt> baps = new ArrayList<Billandpdt>();
		Billandpdt bap = new Billandpdt();
		
		
		sql = "select *from Billandpdt where billId=? and salerecordId=? ";
		Object[] params = new Object[] {billId,salerecordId};
		try {
			manager.connectDB();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			rs = manager.executeQuery(sql, params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			while (rs.next()) {
				bap = new Billandpdt(rs.getInt("id"),rs.getString("billId"),
						rs.getString("salerecordId"),rs.getString("pdtCode")
						);
				baps.add(bap);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		manager.closeDB();
		return baps;
	}
}
